<?php
/**
 * CCrudEventHandler class file.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @copyright Copyright(c) 2010 Lucas Vasconcelos
 * @license BSD License http://www.lucasvasconcelos.com/yap/license
 * @link http://www.lucasvasconcelos.com/yap/
 * @version $Id: CCrudEventHandler.php 21 2010-02-09 19:48:24Z contato@lucasvasconcelos.com $
 * @package yap.web.ui
 */

Prado::using('yap.web.ui.interfaces');

/**
 * CCrudEventHandler class.
 * 
 * Manipuladores de evento padrão usados pela classes que implementam a interface {@link IPageCrud}.
 *
 * @author Lucas Vasconcelos <contato@lucasvasconcelos.com>
 * @version 1.0 $Revision: 21 $
 * @package yap.web.ui
 */
class CCrudEventHandler
{
	public static function create(IPageCrud $sender,$param)
	{
		Prado::trace('CrudEventHandler Create action','yap.web.ui');
		$sender->setAction(CActionType::Create);
		
		Prado::trace('CrudEventHandler changeToPanel()','yap.web.ui');
		$sender->getCrudManager()->changeToPanel(CPanelName::pn_form);
		
		Prado::trace('CrudEventHandler initForm()','yap.web.ui');
		$sender->initForm();
	}
	
	public static function read(IPageCrud $sender,$param)
	{
		Prado::trace('CrudEventHandler Read action','yap.web.ui');
		$sender->setAction(CActionType::Read);
		
		Prado::trace('CrudEventHandler clearCriteria()','yap.web.ui');
		$sender->getGridManager()->clearCriteria();
		
		Prado::trace('CrudEventHandler changeToPanel()','yap.web.ui');
		$sender->getCrudManager()->changeToPanel(CPanelName::pn_grid);
		
		Prado::trace('CrudEventHandler onSetupCriteria()','yap.web.ui');
		$sender->onSetupCriteria(null);
		
		Prado::trace('CrudEventHandler onLoadGrid()','yap.web.ui');
		$sender->onLoadGrid(null);
	}
	
	public static function update(IPageCrud $sender,$param)
	{
		Prado::trace('CrudEventHandler Update action','yap.web.ui');
		$sender->setAction(CActionType::Update);
		
		Prado::trace('CrudEventHandler changeToPanel()','yap.web.ui');
		$sender->getCrudManager()->changeToPanel(CPanelName::pn_form);
		
		Prado::trace('CrudEventHandler initForm()','yap.web.ui');
		$sender->initForm();
		
		Prado::trace('CrudEventHandler onBindFields()','yap.web.ui');
		$sender->onBindFields(null);
	}
	
	public static function delete(IPageCrud $sender,$param)
	{
		Prado::trace('CrudEventHandler Delete action','yap.web.ui');
		$sender->setAction(CActionType::Delete);

		try
		{
			$trans=$sender->getFinder()->getDbConnection()->beginTransaction();
			
			$record=$sender->getRecord();
			
			Prado::trace('CrudEventHandler beforeDelete()','yap.web.ui');
			$sender->beforeDelete($record);
			
			$record->delete();
			
			Prado::trace('CrudEventHandler afterDelete()','yap.web.ui');
			$sender->afterDelete();
			
			$trans->commit();
			$sender->reload();
		}
		catch(Exception $e)
		{
			$trans->rollBack();
			throw $e;
		}
	}
	
	public static function save(IPageCrud $sender,$param)
	{
		try
		{
			$trans=$sender->getFinder()->getDbConnection()->beginTransaction();
			
			if($sender->getAction()===CActionType::Create)
			{
				$class=get_class($sender->getFinder());
				$record=new $class;
				if(property_exists($record,'id'))
					$record->id=null;
				if(property_exists($record,'create_time'))
					$record->create_time=time();
				if(property_exists($record,'update_time'))
					$record->update_time=time();
			}
			else
			{
				$record=$sender->getRecord();
				if(property_exists($record,'update_time'))
					$record->update_time=time();
			}
			
			Prado::trace('CrudEventHandler autoBindRecord()','yap.web.ui');
			$sender->getCrudManager()->autoBindRecord($record,$sender->getAliasId());
			
			Prado::trace('CrudEventHandler beforeSave()','yap.web.ui');
			$sender->beforeSave($record);
			
			$record->save();
			
			Prado::trace('CrudEventHandler afterSave()','yap.web.ui');
			$sender->afterSave($record);
			
			$trans->commit();
			$sender->reload();
		}
		catch(Exception $e)
		{
			$trans->rollBack();
			throw $e;
		}
	}
	
	public static function bindFields(IPageCrud $sender,$param)
	{
		Prado::trace('CrudEventHandler autoBindFields()','yap.web.ui');
		$sender->getCrudManager()->autoBindFields(array(
			$sender->getAliasId() => $sender->getRecord()
		));
	} 
}